#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
@todo
@Author : sunp
@Date :2021/11/25
'''


import time

class Bookself:

    def __init__(self, viewClient):
        if not viewClient:
            raise ValueError(f'viewClient is not null,but now it\'s {viewClient}')
        self.vc = viewClient


    def openBookself(self):
        self.vc.dump()
        RecyclerView = self.vc.findViewWithAttribute(
            'class',
            u'androidx.recyclerview.widget.RecyclerView')
        textView = RecyclerView.children[1]
        text = textView.getText()
        textView.touch()
        print(u'正在阅读{ %s }...'%text)
        self.vc.dump()
        button = self.vc.findViewWithText(u'去书架')
        while not button:
            time.sleep(10)  # 休息翻页10s
            tupCenter = RecyclerView.getCenter()
            self.vc.swipe(startX=tupCenter[0], startY=tupCenter[1], endY=tupCenter[1])
            print(u'正在阅读{ %s }...'%text)
            self.vc.dump()
            button = self.vc.findViewWithText(u'去书架')
        else:
            # 处理 书籍最后章节下一页跳过，到最末页找 `去书架`
            tupCenter = RecyclerView.getCenter()
            self.vc.swipe(startX=tupCenter[0], startY=tupCenter[1], endY=tupCenter[1])
            self.vc.dump()
            button = self.vc.findViewWithText(u'去书架')
            button.touch()
            # 看完本书 在书架中移除
            self.vc.dump()
            textView.longTouch()
            time.sleep(1)
            # # 删除
            self.vc.dump()
            # 底部 text 删除 不起作用，故点击 it’s parent
            self.vc.findViewWithText(u'删除').parent.touch()
            self.vc.dump()
            self.vc.findViewWithText(u'确认').touch()
            self.vc.dump()
            self.openBookself()
